package com.uam.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.uam.core.lk.utils.ResponseResult;
import com.uam.core.lk.utils.annotation.BaseErrorCode;
import com.uam.core.entity.enums.ErrorCode;
import com.uam.core.entity.model.PreviewTrack;
import com.uam.core.service.RouteService;

import lombok.extern.slf4j.Slf4j;

/**
 * 航线管理
 *
 * @author lishf
 */
@Slf4j
@RestController
@RequestMapping("/uam/route")
public class RouteController {

	@Autowired
	private RouteService routeService = null;



	@RequestMapping(value = "/preview", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
			MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE })
	public ResponseResult<List<PreviewTrack>> preview(
			@RequestParam(name = "min_altitude", required = false) double minAltitude,
			@RequestParam(name = "max_altitude", required = false) double maxAltitude,
			@RequestParam(name = "safe_distance", required = false) double safeDistance,
			@RequestParam(name = "speed", required = false) double speed,
			@RequestParam(name = "settings", required = false) String settings) {
		long startTime = System.currentTimeMillis();
		try {
			List<PreviewTrack> result = this.routeService.preview(minAltitude, maxAltitude, safeDistance, speed,
					JSON.parseArray(settings));
			if (result == null) {
				RouteController.log.error("航线预览失败：无预览航线");
				return new ResponseResult<List<PreviewTrack>>(ErrorCode.NO_ROUTE_DATA);
			}
			RouteController.log.info("总共{}个航线预览模式", result.size());
			ResponseResult<List<PreviewTrack>> response = new ResponseResult<List<PreviewTrack>>(result);
			RouteController.log.info("preview cost:{}ms", System.currentTimeMillis() - startTime);
			return response;
		} catch (Throwable e) {
			RouteController.log.error("航线预览异常", e);
			return new ResponseResult<List<PreviewTrack>>(BaseErrorCode.SpringExceptionErrorCode);
		}
	}

}
